package com.xh3140.android.widget.dialog;

import android.content.Context;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.Px;
import androidx.annotation.Size;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import com.xh3140.android.widget.dialog.callback.ConfigParamsCallback;
import com.xh3140.android.widget.dialog.params.BaseParams;
import com.xh3140.android.widget.dialog.params.DialogParams;
import com.xh3140.android.widget.dialog.params.LoadingParams;
import com.xh3140.android.widget.dialog.view.FooterView;
import com.xh3140.android.widget.dialog.view.HeaderView;
import com.xh3140.android.widget.dialog.view.LoadingView;
import com.xh3140.android.widget.dialog.view.RootView;

/**
 * 加载对话框
 */
@SuppressWarnings("unused")
public final class LoadingDialog extends BaseDialog<LoadingView> {
    /**
     * 加载对话框属性
     */
    private final Params mParams;

    /**
     * 加载对话框属性数据类
     */
    private static final class Params {
        public final DialogParams dialog;
        public final BaseParams<LoadingDialog> base;
        public final LoadingParams loading;

        private Params(@NonNull DialogParams dialog, @NonNull BaseParams<LoadingDialog> base, @NonNull LoadingParams loading) {
            this.dialog = dialog;
            this.base = base;
            this.loading = loading;
        }
    }

    private LoadingDialog(@NonNull Params params) {
        super(params.dialog);
        mParams = params;
    }


    @NonNull
    @Override
    protected RootView<LoadingView> initRootView(@NonNull Context context) {
        final LoadingDialog dialog = this;
        return new RootView<LoadingView>(context) {
            @Override
            protected HeaderView initHeaderView(@NonNull Context context) {
                return new HeaderView(context, mParams.base.header);
            }

            @Override
            protected LoadingView initBodyView(@NonNull Context context) {
                return new LoadingView(context, mParams.loading);
            }

            @Override
            protected FooterView initFooterView(@NonNull Context context) {
                return new FooterView(context, dialog, mParams.base.footer);
            }
        };
    }

    /**
     * 加载对话框构建器
     * 设置输入对话框属性
     */
    public static final class Builder extends BaseDialog.Builder<LoadingDialog, Builder> {
        /**
         * 对话框加载主体视图配置属性
         */
        private final LoadingParams mLoadingParams;

        public Builder(@NonNull Context context, @IntRange(from = 0) int buttonCount) {
            super(context, buttonCount);
            mLoadingParams = new LoadingParams(context);
            mDialogParams.showDividers = -1;
        }

        public Builder(@NonNull FragmentActivity activity, @IntRange(from = 0) int buttonCount) {
            super(activity, buttonCount);
            mLoadingParams = new LoadingParams(mDialogParams.context);
            mDialogParams.showDividers = -1;
        }

        public Builder(@NonNull Fragment fragment, @IntRange(from = 0) int buttonCount) {
            super(fragment, buttonCount);
            mLoadingParams = new LoadingParams(mDialogParams.context);
            mDialogParams.showDividers = -1;
        }

        /**
         * 构建对话框
         * 这个方法应该在所有配置结束后调用
         */
        @Override
        public LoadingDialog build() {
            return new LoadingDialog(new Params(mDialogParams, mBaseParams, mLoadingParams));
        }

        /**
         * 配置加载主体视图属性
         *
         * @param callback 配置回调
         */
        public Builder configLoadingParams(@NonNull ConfigParamsCallback<LoadingParams> callback) {
            callback.callback(mLoadingParams);
            return this;
        }


        /**
         * 设置加载视图是否是旋转样式
         *
         * @param spinner 是否是旋转样式
         */
        public Builder setLoadingSpinner(boolean spinner) {
            mLoadingParams.isSpinner = spinner;
            return this;
        }

        /**
         * 设置加载视图是否是无限的
         *
         * @param infinite 是否是无限的
         */
        public Builder setLoadingInfinite(boolean infinite) {
            mLoadingParams.isInfinite = infinite;
            return this;
        }

        /**
         * 设置旋转视图高度 px
         *
         * @param height 高度
         */
        public Builder setLoadingSpinnerHeight(@Px int height) {
            mLoadingParams.spinnerHeight = height;
            return this;
        }

        /**
         * 水平视图高度 px
         *
         * @param height 高度
         */
        public Builder setLoadingHorizontalHeight(@Px int height) {
            mLoadingParams.horizontalHeight = height;
            return this;
        }

        /**
         * 加载视图进度条背景资源ID
         *
         * @param resId 资源ID
         */
        public Builder setLoadingDrawable(@DrawableRes int resId) {
            mLoadingParams.drawable = resId;
            return this;
        }

        /**
         * 加载视图进度条最大刻度
         *
         * @param max 最大刻度
         */
        public Builder setLoadingMax(int max) {
            mLoadingParams.max = max;
            return this;
        }

        /**
         * 加载视图进度条刻度
         *
         * @param progress 刻度
         */
        public Builder setLoadingProgress(int progress) {
            mLoadingParams.progress = progress;
            return this;
        }

        /**
         * 加载视图颜色 argb
         *
         * @param color 颜色
         */
        public Builder setLoadingTintColor(@ColorInt int color) {
            mLoadingParams.tintColor = color;
            return this;
        }

        /**
         * 设置加载视图外边距
         * 格式：[start, top, end, bottom] px
         *
         * @param margins 边距
         */
        public Builder setLoadingMargins(@Nullable @Size(4) int[] margins) {
            mLoadingParams.margins = margins;
            return this;
        }

        /**
         * 设置加载视图外边距
         *
         * @param margins 边距 px
         */
        public Builder setLoadingMargins(@Px int margins) {
            mLoadingParams.margins = new int[]{margins, margins, margins, margins};
            return this;
        }

        /**
         * 设置加载视图外边距
         *
         * @param left   左边距 px
         * @param top    上边距 px
         * @param right  右边距 px
         * @param bottom 下边距 px
         */
        public Builder setLoadingMargins(@Px int left, @Px int top, @Px int right, @Px int bottom) {
            mLoadingParams.margins = new int[]{left, top, right, bottom};
            return this;
        }

        /**
         * 设置加载视图文本位置是否在上方
         *
         * @param isTop 是否在上方
         */
        public Builder setLoadingSubtitleTop(boolean isTop) {
            mLoadingParams.isTopSubtitle = isTop;
            return this;
        }

        /**
         * 设置加载视图文本
         *
         * @param text 文本
         */
        public Builder setLoadingSubtitleText(@Nullable CharSequence text) {
            mLoadingParams.text = text;
            return this;
        }

        /**
         * 设置加载视图文本位置
         *
         * @param gravity 位置
         */
        public Builder setLoadingSubtitleGravity(int gravity) {
            mLoadingParams.gravity = gravity;
            return this;
        }

        /**
         * 设置加载视图文本字体大小 sp
         *
         * @param textSize 字体大小
         */
        public Builder setLoadingSubtitleTextSize(float textSize) {
            mLoadingParams.textSize = textSize;
            return this;
        }

        /**
         * 设置加载视图文本字体颜色 argb
         *
         * @param textColor 字体颜色
         */
        public Builder setLoadingSubtitleTextColor(@ColorInt int textColor) {
            mLoadingParams.textColor = textColor;
            return this;
        }

        /**
         * 设置加载视图文本字体样式
         *
         * @param typeface 字体样式
         */
        public Builder setLoadingSubtitleTypeface(int typeface) {
            mLoadingParams.typeface = typeface;
            return this;
        }

        /**
         * 设置加载视图背景颜色 argb
         *
         * @param backgroundColor 背景颜色
         */
        public Builder setLoadingSubtitleBackgroundColor(@ColorInt int backgroundColor) {
            mLoadingParams.backgroundColor = backgroundColor;
            return this;
        }

        /**
         * 设置加载视图高度 px
         *
         * @param height 高度
         */
        public Builder setLoadingSubtitleHeight(@Px int height) {
            mLoadingParams.height = height;
            return this;
        }

        /**
         * 设置加载视图内边距
         * 格式：[start, top, end, bottom] px
         *
         * @param padding 边距
         */
        public Builder setLoadingSubtitlePadding(@Nullable @Size(4) int[] padding) {
            mLoadingParams.padding = padding;
            return this;
        }

        /**
         * 设置加载视图内边距
         *
         * @param padding 边距 px
         */
        public Builder setLoadingSubtitlePadding(@Px int padding) {
            mLoadingParams.padding = new int[]{padding, padding, padding, padding};
            return this;
        }

        /**
         * 设置加载视图内边距
         *
         * @param left   左边距 px
         * @param top    上边距 px
         * @param right  右边距 px
         * @param bottom 下边距 px
         */
        public Builder setLoadingSubtitlePadding(@Px int left, @Px int top, @Px int right, @Px int bottom) {
            mLoadingParams.padding = new int[]{left, top, right, bottom};
            return this;
        }
    }
}